package com.omesoft.hksnore.util.dao;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.omesoft.hksnore.util.dbhelper.DBHelper4Sleep;
import com.omesoft.hksnore.util.dbhelper.SetData;
import com.omesoft.hksnore.util.entity.Medix_Pub_Sync_SleepDetail;

import java.util.ArrayList;
import java.util.List;

public class MendaleSleepIfcImpl implements MendaleSleepIfc {
	DBHelper4Sleep db = null;
	String table = SetData.TABLE_NAME_MEDIX_PUB_SYNC_SLEEPDETAIL;
	Context context;

	public MendaleSleepIfcImpl(Context context) {
		db = DBHelper4Sleep.getInstance(context, SetData.DATATBASE_NAME_MendaleSleep);
		this.context = context;
	}

	// memberID在中已经设置。
	@Override
	public void insert(Medix_Pub_Sync_SleepDetail dto) {
		db.insertMendaleSleep(dto, table);

	}

	// memberID已添加
	@Override
	public List<Medix_Pub_Sync_SleepDetail> findAll(String SleepRecordID, int memberID) {
		List<Medix_Pub_Sync_SleepDetail> dtos = new ArrayList<Medix_Pub_Sync_SleepDetail>();
		Cursor cursor = db.findDayMendaleSleep(table, SleepRecordID, memberID);
		while (cursor.moveToNext()) {
			Medix_Pub_Sync_SleepDetail dto = new Medix_Pub_Sync_SleepDetail();
			dto.setSampleCount(cursor.getInt(cursor.getColumnIndexOrThrow("SampleCount")));
			dto.setActCount(cursor.getInt(cursor.getColumnIndexOrThrow("ActCount")));
			dto.setTurnCount(cursor.getInt(cursor.getColumnIndexOrThrow("TurnCount")));
			dto.setSleepQuality(cursor.getFloat(cursor.getColumnIndexOrThrow("SleepQuality")));
			dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
			dto.setRecordDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
			dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
			dto.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
			dto.setSnoreStopCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreStopCount")));
			dtos.add(dto);
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return dtos;
	}

	// memberID已添加
	@Override
	public String findLastDate(int MemberID, String ID) {
		Cursor cursor = db.findLastDate(table, MemberID, ID);
		String RecordDate = null;
		while (cursor.moveToNext()) {
			RecordDate = cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate"));
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return RecordDate;
	}

	@Override
	public List<Medix_Pub_Sync_SleepDetail> findAllSleepDetaiByMemberId(int memberID, String timestamp) {
		// TODO Auto-generated method stub
		List<Medix_Pub_Sync_SleepDetail> dtos = new ArrayList<Medix_Pub_Sync_SleepDetail>();
		Cursor cursor = db.findMendaleSleepDetailByMemberId(table, memberID, timestamp);
		while (cursor.moveToNext()) {
			Medix_Pub_Sync_SleepDetail dto = new Medix_Pub_Sync_SleepDetail();
			dto.setMemberID(cursor.getInt(cursor.getColumnIndexOrThrow("MemberID")));
			dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
			dto.setSleepDetailID(cursor.getString(cursor.getColumnIndexOrThrow("SleepDetailID")));
			dto.setSleepQuality(cursor.getFloat(cursor.getColumnIndexOrThrow("SleepQuality")));
			dto.setSampleCount(cursor.getInt(cursor.getColumnIndexOrThrow("SampleCount")));
			dto.setActCount(cursor.getInt(cursor.getColumnIndexOrThrow("ActCount")));
			dto.setTurnCount(cursor.getInt(cursor.getColumnIndexOrThrow("TurnCount")));
			dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
			dto.setUpdatedDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
			dto.setIsDeleted(cursor.getInt(cursor.getColumnIndexOrThrow("IsDeleted")));
			dto.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
			dto.setSnoreStopCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreStopCount")));
			Log.v("Medix_Pub_SleepDetail", "dto:::" + dto.toString());
			dtos.add(dto);
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return dtos;
	}

	public List<Medix_Pub_Sync_SleepDetail> findAllSleepDetaiByMemberId(int memberID) {
		// TODO Auto-generated method stub
		List<Medix_Pub_Sync_SleepDetail> dtos = new ArrayList<Medix_Pub_Sync_SleepDetail>();
		Cursor cursor = db.findMendaleSleepDetailByMemberId(table, memberID);
		while (cursor.moveToNext()) {
			Medix_Pub_Sync_SleepDetail dto = new Medix_Pub_Sync_SleepDetail();
			dto.setMemberID(cursor.getInt(cursor.getColumnIndexOrThrow("MemberID")));
			dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
			dto.setSleepDetailID(cursor.getString(cursor.getColumnIndexOrThrow("SleepDetailID")));
			dto.setSleepQuality(cursor.getFloat(cursor.getColumnIndexOrThrow("SleepQuality")));
			dto.setSampleCount(cursor.getInt(cursor.getColumnIndexOrThrow("SampleCount")));
			dto.setActCount(cursor.getInt(cursor.getColumnIndexOrThrow("ActCount")));
			dto.setTurnCount(cursor.getInt(cursor.getColumnIndexOrThrow("TurnCount")));
			dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
			dto.setUpdatedDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
			dto.setIsDeleted(cursor.getInt(cursor.getColumnIndexOrThrow("IsDeleted")));
			dto.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
			dto.setSnoreStopCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreStopCount")));
			Log.e("---------", "dto:::" + dto.toString());
			dtos.add(dto);
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return dtos;
	}
	public List<Medix_Pub_Sync_SleepDetail> findAllSleepDetaiBySleepId(String SleepId) {
		// TODO Auto-generated method stub
		List<Medix_Pub_Sync_SleepDetail> dtos = new ArrayList<Medix_Pub_Sync_SleepDetail>();
		Cursor cursor = db.findMendaleSleepDetailBySleepId(table, SleepId);
		while (cursor.moveToNext()) {
			Medix_Pub_Sync_SleepDetail dto = new Medix_Pub_Sync_SleepDetail();
			dto.setMemberID(cursor.getInt(cursor.getColumnIndexOrThrow("MemberID")));
			dto.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
			dto.setSleepDetailID(cursor.getString(cursor.getColumnIndexOrThrow("SleepDetailID")));
			dto.setActCount(cursor.getInt(cursor.getColumnIndexOrThrow("ActCount")));
			dto.setTurnCount(cursor.getInt(cursor.getColumnIndexOrThrow("TurnCount")));
			dto.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
			dto.setUpdatedDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
			dto.setIsDeleted(cursor.getInt(cursor.getColumnIndexOrThrow("IsDeleted")));
			dto.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
			dto.setSnoreStopCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreStopCount")));
			dto.setBreathRate(cursor.getInt(cursor.getColumnIndexOrThrow("BreathRate")));
			dto.setSnoreDuration(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreDuration")));
			dtos.add(dto);
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return dtos;
	}

	@Override
	public void insertOrUpdate(Medix_Pub_Sync_SleepDetail dto) {
		// TODO Auto-generated method stub
		db.InsertorUpdateMendaleSleep(dto, table);
	}

	@Override
	public void insertOrUpdate(List<Medix_Pub_Sync_SleepDetail> dtos) {
		// TODO Auto-generated method stub
		db.InsertorUpdateMendaleSleep(dtos, table);
	}

	@Override
	public ArrayList<String> findAllSleepID(int MemberId) {
		// TODO Auto-generated method stub
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = db.findSleepIDByMemberId(SetData.TABLE_NAME_MEDIX_PUB_SYNC_SLEEP, MemberId);
		while (cursor.moveToNext()) {
			// 去掉sleepdetail已经存在的数据
			Cursor sleepDetail = db.findBySleepID(SetData.TABLE_NAME_MEDIX_PUB_SYNC_SLEEPDETAIL, MemberId,
					cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
			if (sleepDetail == null || sleepDetail.getCount() <= 0) {
				arrayList.add(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
			}
			if (sleepDetail != null) {
				sleepDetail.close();
				sleepDetail = null;
			}
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
		return arrayList;
	}

	@Override
	public void updateStartTime(String ID, String startTime) {
		// TODO Auto-generated method stub
		db.updateStartTime2Sleep(ID, startTime);
	}

	@Override
	public int findTotal(String SleepRecordID, int memberID) {
		// TODO Auto-generated method stub
		Cursor cursor = db.findAllTotal(table, memberID, SleepRecordID);
		int total = 0;
		while (cursor.moveToNext()) {
			total = cursor.getInt(cursor.getColumnIndexOrThrow("Count"));
		}
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
//		Log.e("MendaleSleepAllIfcImpl", "睡眠详情::total=" + total);
		return total;
	}


}
